

import UIKit

class WX_ZhifuView: UIView { //WXApiDelegate
    var orderId:String!
    var jifen:String!
    var money:String!
    var price:UILabel!
    var weixinBtn:UIButton!
    var zfbBtn:UIButton!
    var jifenBtn:UIButton!
    var wxCCImg:UIImageView!
    var zfbCCImg:UIImageView!
    var jifenCCImg:UIImageView!
    var payBtn:UIButton!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor=UIColor.black.withAlphaComponent(0.5)
        
        let boxview=UIView.init(frame: CGRect(x: 0, y: SCREEN_HEIGHT-255-BottomSpaceHeigh, width: SCREEN_WIDTH, height: 255+BottomSpaceHeigh))
        boxview.backgroundColor=UIColor.white
        self.addSubview(boxview)
        
        let close=UIButton.init(frame: CGRect(x: frame.size.width-50, y: 0, width: 50, height: 48))
        close.setImage(UIImage(named: "支付方式-关闭"), for: .normal)
        close.addTarget(self, action: #selector(closeView), for: .touchUpInside)
        boxview.addSubview(close)
        
        let title=UILabel.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 48))
        title.text="选择支付方式"
        title.font=UIFont.boldSystemFont(ofSize: 18)
        title.textAlignment = .center
        title.textColor=UIColor.init(hexString: "222222")
        boxview.addSubview(title)
        
//        price=UILabel.init(frame: CGRect(x: 0, y: 64, width: SCREEN_WIDTH, height: 22))
//        price.font=UIFont.boldSystemFont(ofSize: 24)
//        price.textAlignment = .center
//        price.textColor=UIColor.init(hexString: "222222")
//        boxview.addSubview(price)
        
        //wx
        
        let line1=UIView.init(frame: CGRect(x: 0, y: 63, width: SCREEN_WIDTH-30, height: 1))
        line1.backgroundColor=UIColor.init(hexString: "ebebeb")
        boxview.addSubview(line1)
        
        weixinBtn=UIButton.init(frame: CGRect(x: 15, y: 64+15, width: SCREEN_WIDTH-15, height: 47))
        boxview.addSubview(weixinBtn)
        weixinBtn.isSelected=true
      //  weixinBtn.addTarget(self, action: #selector(chooseWX), for: .touchUpInside)
        
        let wxImg=UIImageView.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        wxImg.image=UIImage(named: "微信支付")
        weixinBtn.addSubview(wxImg)
        
        let wxTitle=UILabel.init(frame: CGRect(x: 41, y: 2, width: 50, height: 12))
        wxTitle.text="微信支付"
        wxTitle.font=UIFont.systemFont(ofSize: 12)
        wxTitle.textColor=UIColor.init(hexString: "333333")
        weixinBtn.addSubview(wxTitle)
        
        let wxLab=UILabel.init(frame: CGRect(x: 41, y: 19, width: 120, height: 9))
        wxLab.text="亿万用户的选择，更快更安全"
        wxLab.font=UIFont.systemFont(ofSize: 9)
        wxLab.textColor=UIColor.init(hexString: "8c8c8c")
        weixinBtn.addSubview(wxLab)
        
        let wximg2=UIImageView.init(frame: CGRect(x: 96, y: 2, width: 20, height: 12))
        wximg2.image=UIImage(named: "推荐")
        weixinBtn.addSubview(wximg2)
        
        wxCCImg=UIImageView.init(frame: CGRect(x: weixinBtn.frame.size.width-30, y: 8, width: 14, height: 14))
        wxCCImg.image=UIImage(named: "勾选-选中")
        wxCCImg.contentMode = .scaleAspectFit
        wxCCImg.isUserInteractionEnabled=true
        weixinBtn.addSubview(wxCCImg)
        
        let line2=UIView.init(frame: CGRect(x: 0, y: 140, width: SCREEN_WIDTH-30, height: 1))
        line2.backgroundColor=UIColor.init(hexString: "ebebeb")
        boxview.addSubview(line2)
        
        
    

        
        
        //pay
        payBtn=UIButton.init(frame: CGRect(x: 15, y: 140+40, width: SCREEN_WIDTH-30, height: 45))
        payBtn.layer.cornerRadius=5
        payBtn.backgroundColor=UIColor.init(hexString: "ff6635")
        payBtn.addTarget(self, action: #selector(gopay), for: .touchUpInside)
        payBtn.titleLabel?.font=UIFont.systemFont(ofSize: 15)
        payBtn.setTitleColor(UIColor.white, for: .normal)
        payBtn.setTitle("去支付", for: .normal)
        boxview.addSubview(payBtn)
        
        //监听微信支付通知
        NotificationCenter.default.addObserver(self, selector: #selector(paySuccess), name: NSNotification.Name("WXPaySuccessNotification"), object: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func closeView(){
        self.removeFromSuperview()
    }
    
    @objc func  chooseZFB(){
    //    zfbBtn.isSelected=true
        weixinBtn.isSelected=false
        jifenBtn.isSelected=false
        wxCCImg.image=UIImage(named: "未勾选")
        jifenCCImg.image=UIImage(named: "未勾选")
        zfbCCImg.image=UIImage(named: "勾选-选中")
        payBtn.setTitle("支付宝支付", for: .normal)
        price.text=money
    }
    @objc func chooseWX(){
        zfbBtn.isSelected=false
        weixinBtn.isSelected=true
        jifenBtn.isSelected=false

        wxCCImg.image=UIImage(named: "勾选-选中")
        zfbCCImg.image=UIImage(named: "未勾选")
        jifenCCImg.image=UIImage(named: "未勾选")
        payBtn.setTitle("微信支付", for: .normal)
        price.text=money

    }
    @objc func chooseJifen(){
        zfbBtn.isSelected=false
        weixinBtn.isSelected=false
        jifenBtn.isSelected=true

        jifenCCImg.image=UIImage(named: "勾选-选中")
        zfbCCImg.image=UIImage(named: "未勾选")
        wxCCImg.image=UIImage(named: "未勾选")
        payBtn.setTitle("积分支付", for: .normal)
        price.text=jifen

    }
    
    @objc func gopay(){


        
            let dic=["order_id":orderId!,"method":"app"] as [String : Any]
            Network.request(.wxPay(dic)) { (JSON) in
              // 调起微信支付
                
                let req=PayReq.init()
                req.partnerId  = JSON["data"]["partnerid"].string ?? ""
                req.prepayId    = JSON["data"]["prepayid"].string ?? ""
                req.nonceStr    = JSON["data"]["noncestr"].string ?? ""
                req.timeStamp  = UInt32(Int32(JSON["data"]["timestamp"].string ?? "0") ?? 0)
                req.package      = JSON["data"]["package"].string ?? ""
                req.sign    = JSON["data"]["sign"].string ?? ""
                WXApi.send(req, completion: nil)
              

            } failure: { (err) in

            }

        
        
        
    }
    
    @objc func paySuccess() {
        YProgressHUD.showToast("支付成功")
        self.removeFromSuperview()
        let cvc=getCurrentVc()
        cvc.navigationController?.popViewController(animated: true)
    }
    
  
    
}
